// pages/detail/detail.js
const app = getApp();
import api from '../../api/index.js'
const { request } = require("../../utils/wx-api.js")
//引入wxParse文件
const WxParse = require('../../wxParse/wxParse.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gallery: '',
    info: '',
    comment: '',
    productList:'',
    attribute:'',
    issue:'',
    relatedGoods:''
  },
  addCart() { //添加入购物车
    console.log(app.globalData);
    let that = this;
    if(!app.globalData.userInfo){  //未登录不能添加
      wx.showToast({
        title: '请登录！',
        icon:'none',
        success(res){
          setTimeout(()=>{
            wx.switchTab({
              url: '../mine/mine',
              success(res){
                app.globalData.redirectUrl=that.data.info.id;
                // console.log('switch',app);
              }
            })
          },1500)
        }
      })
    }else {
      let goodsId = this.data.productList[0].goods_id;
      // let number = this.data.info.goods_number;
      let productId = this.data.productList[0].id;
      request({
        url: api.cartAdd
      },
      {
        goodsId,
        number: 1,
        productId
      },'POST', { 'X-Nideshop-Token': app.globalData.token })
      .then(res => {
        // debugger
        console.log(res);
        wx.showToast({
          title: '加入购物车成功'
        })
      })
    }
  },
  buy(){  //立即购买
    // console.log('buy',app)
    if(app.globalData.userInfo){
      if (!app.globalData.addressId) {
        wx.navigateTo({
          url: '../addressManage/addressManage'
        })
      } else {//已登录并且获取到地址信息，即可跳转到购物车提交订单,
        let goodsId = this.data.productList[0].goods_id;
        // let number = this.data.info.goods_number;
        let productId = this.data.productList[0].id;
        request({
          url: api.cartAdd
        },
          {
            goodsId,
            number: 1,
            productId
          }, 'POST', { 'X-Nideshop-Token': app.globalData.token })
          .then(res => {
            // debugger
            console.log(res);
            wx.switchTab({
              url: '../shopcar/shopcar'
            })
          })
      }
    }else {
      let that = this;
      wx.showToast({
        title: '请登录！',
        icon: 'none',
        success(res) {
          setTimeout(() => {
            wx.switchTab({
              url: '../mine/mine',
              success(res) {
                app.globalData.redirectUrl = that.data.info.id;
              }
            })
          }, 1500)
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    // console.log(options);
    let {id} = options;
    request({ url: api.goodsDetail }, { id })//1009024
    .then(res => {
      // console.log(res);
      let { gallery } = res.data.data;
      let { info } = res.data.data;
      let { comment } = res.data.data;
      let {productList} = res.data.data;
      let {attribute} = res.data.data;
      let {issue} = res.data.data;
      this.setData({
        gallery,
        info,
        comment,
        productList,
        attribute,
        issue
      })
      let goods_desc = info.goods_desc;
      WxParse.wxParse('goods_desc','html',goods_desc,that,0);
      //请求相关商品的数据
      request({url:api.goodsRelated},{id})
      .then(res=>{
        let relatedGoods = res.data.data.goodsList;
        this.setData({
          relatedGoods
        })
        // console.log(res);
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})